package com.wuseguang.springcloud.controller;


import com.wuseguang.springcloud.Filter.RequestParameterWrapper;
import com.wuseguang.springcloud.entity.Roles;
import com.wuseguang.springcloud.service.RolesService;
import com.wuseguang.springcloud.util.CodeUtil;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author
 * @since 2019-11-27
 */
@RestController
@RequestMapping("/roles")
public class RolesController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private RolesService rolesServiceImpl;


    /**
     * 查询学校角色
     * @param request
     * @param type 1教师，2学生 默认查询教师
     * @return
     */
    @RequestMapping("/findSchoolRoles")
    public Result findSchoolRoles(RequestParameterWrapper request,
                                  @RequestParam(value = "type",required = false,defaultValue = "1") Integer type){
        logger.info("查询学校的角色 RolesController - > findSchoolRoles  type = {}",type);
        //学校id
        String schoolId = request.getParam("schoolId");

        return rolesServiceImpl.findSchoolRoles(schoolId,type);
    }


    /**
     * 查询权限管理列表
     *
     * @param request
     * @return
     */
    @RequestMapping("/findRolePermissions")
    public Result findRolePermissions(RequestParameterWrapper request,
                                      @RequestParam(value = "indexNo", required = false, defaultValue = "1") Integer indexNo,
                                      @RequestParam(value = "rows", required = false, defaultValue = "20") Integer rows) {

        logger.info("查询权限管理 RolesController.class->findRolePermissions()");
        //学校id
        String schoolId = request.getParam("schoolId");

        return rolesServiceImpl.findRolePermissions(schoolId, indexNo, rows);
    }

    /**
     * 查询部门教师
     *
     * @param request
     * @return
     */
    @RequestMapping("/findDepartmentTeacher")
    public Result findDepartmentTeacher(RequestParameterWrapper request) {

        logger.info("查询部门教师 RolesController.class->findDepartmentTeacher()");
        //学校id
        String schoolId = request.getParam("schoolId");

        return rolesServiceImpl.findDepartmentTeacher(schoolId);
    }

    /**
     * 查询角色的功能权限
     *
     * @param request
     * @param roleId  角色id
     * @return
     */
    @RequestMapping("/findRoleFunction")
    public Result findRoleFunction(RequestParameterWrapper request,
                                   @RequestParam(value = "roleId", required = false) Integer roleId) {

        logger.info("查询角色的功能权限 RolesController.class->findRoleFunction()");
        //判断角色是否为空
        if (roleId == null) {
            return new Result(CodeUtil.REQUIRED_VALUE_IS_EMPTY.getCode(), CodeUtil.REQUIRED_VALUE_IS_EMPTY.getMsg() + "[角色为空]");
        }
        //学校id
        String schoolId = request.getParam("schoolId");

        return rolesServiceImpl.findRoleFunction(roleId, schoolId);
    }

    /**
     * 给角色添加权限
     *
     * @param request
     * @param groupingId     组id
     * @param jurisdictionId 权限id
     * @return
     */
    @RequestMapping("/addAndDeleteRoleJurisdiction")
    public Result addAndDeleteRoleJurisdiction(RequestParameterWrapper request,
                                               @RequestParam(value = "groupingId", required = false) Integer groupingId,
                                               @RequestParam(value = "jurisdictionId", required = false) Integer[] jurisdictionId) {

        logger.info("给角色添加权限 RolesController.class-> addRoleJurisdiction()");
        //学校id
        String schoolId = request.getParam("schoolId");

        return rolesServiceImpl.addAndDeleteRoleJurisdiction(groupingId, jurisdictionId, schoolId);
    }

    /**
     * 修改角色名称
     *
     * @param request
     * @param roleId  角色id
     * @return
     */
    @RequestMapping("/updateRoleName")
    public Result updateRoleName(RequestParameterWrapper request,
                                 @RequestParam(value = "roleId", required = false) Integer roleId,
                                 @RequestParam(value = "name", required = false) String name,
                                 @RequestParam(value = "schoolId") Integer schoolId) {

        logger.info("修改角色名称 RolesController.class-> updateRoleName()");

        //判断是否为空
        if (StringUtils.isEmpty(roleId)) {
            logger.info("roleId 为空");
            return new Result(CodeUtil.REQUIRED_VALUE_IS_EMPTY.getCode(), CodeUtil.REQUIRED_VALUE_IS_EMPTY.getMsg() + "[角色为空]");
        }
        //判断是否为空
        if (StringUtils.isEmpty(name)) {
            logger.info("name 为空");
            return new Result(CodeUtil.REQUIRED_VALUE_IS_EMPTY.getCode(), CodeUtil.REQUIRED_VALUE_IS_EMPTY.getMsg() + "[角色名字为空]");
        }
        //判断是否为默认
        if (schoolId == 0) {
            logger.info("默认选项");
            return new Result(CodeUtil.THE_DEFAULT_OPTION_IS_NOT_OPERATIONAL);
        }

        return rolesServiceImpl.updateRoleName(roleId, name, schoolId);
    }

    /**
     * 删除角色
     *
     * @param request
     * @param roleId   角色id
     * @param schoolId 学校id
     * @return
     */
    @RequestMapping("/deleteRole")
    public Result deleteRole(RequestParameterWrapper request,
                             @RequestParam(value = "roleId", required = false) Integer roleId,
                             @RequestParam(value = "schoolId") Integer schoolId) {

        logger.info("修改角色名称 RolesController.class-> deleteRole()");

        //判断是否为空
        if (StringUtils.isEmpty(roleId)) {
            logger.info("roleId 为空");
            return new Result(CodeUtil.REQUIRED_VALUE_IS_EMPTY.getCode(), CodeUtil.REQUIRED_VALUE_IS_EMPTY.getMsg() + "[角色为空]");
        }
        //判断是否为默认
        if (schoolId == 0) {
            logger.info("默认选项");
            return new Result(CodeUtil.THE_DEFAULT_OPTION_IS_NOT_OPERATIONAL);
        }

        return rolesServiceImpl.deleteRole(roleId);
    }

    /**
     * 添加角色  不开fegin测试需要把@RequestBody删掉
     *
     * @param request
     * @param roles
     * @return
     */
    @RequestMapping("/addRole")
    public Result addRole(RequestParameterWrapper request,Roles roles) {

        logger.info("添加角色 RolesController.class-> addRole()");
        //学校id
        String schoolId = request.getParam("schoolId");
        roles.setSchoolId(Integer.valueOf(schoolId));
        return rolesServiceImpl.addRole(roles);
    }

}

